import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:syn_4s/core/utils/mqtt_manager.dart';
import 'package:syn_4s/ui/shared/app_theme.dart';
import 'package:syn_4s/ui/shared/size_fit.dart';

import 'core/constants/sp_key.dart';
import 'core/router/router.dart';
import 'core/utils/foreground_task_util.dart';
import 'core/utils/sp_utils.dart';
import 'core/viewmodel/language_view_model.dart';
import 'generated/l10n.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  bool isLoggedIn = false;
  String? token = await SPUtils.get(keyToken) ?? "";
  isLoggedIn = token?.isNotEmpty ?? false;
  await initService();
  runApp(MyApp(isLoggedIn));
}

class MyApp extends StatelessWidget {
  final bool isLoggedIn;

  MyApp(this.isLoggedIn, {super.key});

  @override
  Widget build(BuildContext context) {
    SizeFit.initialize(context);
    Get.put(LanguageViewModel());
    return GetMaterialApp(
      title: 'Syn4S',
      // 主题
      theme: AppTheme.norTheme,
      // 路由
      initialRoute: isLoggedIn ? MyRouter.mainRoute : MyRouter.loginRoute,
      routes: MyRouter.routes,
      onGenerateRoute: MyRouter.generateRoute,
      onUnknownRoute: MyRouter.unknownRoute,
      supportedLocales: S.delegate.supportedLocales,
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        S.delegate,
      ],
      debugShowCheckedModeBanner: false,
    );
  }
}
